<?php

namespace addons\shopro\controller;



use think\Db;

class Questionnaire extends Base
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $data=array();
        $questionnaire=Db::name('questionnaire')
            ->where('deletetime',null)
            ->select();
        if(!empty($questionnaire))$data=$questionnaire;

        $this->success('问卷列表', $data);
    }

    public function info()
    {
        $params = $this->request->post();
        $id=$params['id'];
        $info=Db::name('questionnaire')
            ->where('id',$id)
            ->find();
        $data=$info;

        $this->success('问卷详情', $data);
    }

    //题目
    public function issue()
    {
        $params = $this->request->post();
        $id=$params['id'];
        $type=$params['type'];
        $data=array();
        $questionnaire_data=array();
        $count=0;

        $info=Db::name('questionnaire')
            ->where('id',$id)
            ->find();
        $data['info']=$info;

        $num=0;//问卷已做次数
        $user = $this->auth->getUser();
        $user_id=$user->id;
        $questionnaire_user=Db::name('questionnaire_user')
            ->where('questionnaire_id',$id)
            ->where('user_id',$user_id)
            ->order('num desc')
            ->find();
        if(!empty($questionnaire_user))$num=$questionnaire_user['num'];
        $questionnaire_user_list=array();
        if($num>=3){//最后填的问卷
            $questionnaire_user_list=Db::name('questionnaire_user')
                ->where('questionnaire_id',$id)
                ->where('user_id',$user_id)
                ->where('num',3)
                ->select();
        }


        $where='';
//        if($type==1){
//            $where='is_multiple=0';
//        }else{
//            $where='is_multiple=1';
//        }

        $questionnaire=Db::name('questionnaire_issue')->alias('i')
            ->join('questionnaire_option o','o.issue_id=i.id')
            ->where('i.questionnaire_id',$id)
            ->where($where)
            ->field('i.id,i.title,i.is_multiple,o.id as o_id,o.title as o_title,o.is_correct')
            ->select();

        $id_arr=array();
        $hidden=false;
        foreach ($questionnaire as $key2=>$info){
            if(!in_array($info['id'],$id_arr)){
                $questions=array();
                $questions['id']=$info['id'];
                $questions['title']=$info['title'];//问题
                $questions['is_multiple']=$info['is_multiple'];//是否多选
                $questions['hidden']=$hidden;//是否隐藏，前端用
                if($hidden==false)$hidden=true;
                $questions['progress']=0;//进度，前端用
                $questions['option']=array();
                $id_arr[]=$info['id'];
                $questionnaire_data[]=$questions;
            }

            foreach ($questionnaire_data as $key=>$v){
                if($v['id']==$info['id']){
                    $option=array();
                    $option['id']=$info['o_id'];
                    $option['value']=$info['o_title'];
                    $option['is_correct']=$info['is_correct'];
                    $option['avatar']=0;//是否选中

                    if($num>=3){//判断选项是否已选
                        foreach ($questionnaire_user_list as $qul){
                            if($qul['issue_id']==$info['id']){
                                $value=array();
                                if(!empty($qul['value']))$value=explode(",",$qul['value']);
                                if(in_array($info['o_id'],$value))$option['avatar']=1;//是否选中
                            }
                        }
                    }
                    $questionnaire_data[$key]['option'][]=$option;
                }
            }

        }

        $count=count($questionnaire_data);
        $progress=0;
        if($count!=0){$progress=round(100/$count,0);}
        foreach ($questionnaire_data as $key=>$v){
            if($key+1<$count){
                $questionnaire_data[$key]['progress']=$progress*($key+1);
            }else{
                $questionnaire_data[$key]['progress']=100;
            }
        }


        $data['count']=$count;
        $data['questionnaire']=$questionnaire_data;
        $data['num']=$num;

        $this->success('题目', $data);
    }

    //提交问卷
    public function submit()
    {
        $params = $this->request->post();
        $user = $this->auth->getUser();
        $user_id=$user->id;
        $questionnaire_id=$params['id'];
        $questions=$params['questions'];
        $num=0;
        foreach ($questions as $info) {
            foreach ($info['option'] as $option) {
                if ($option['avatar'] == 1&&$option['is_correct']==0) {
                    $num++;
                }
            }
        }

        $num2=1;//问卷已做次数
        $questionnaire_user=Db::name('questionnaire_user')
            ->where('questionnaire_id',$questionnaire_id)
            ->where('user_id',$user_id)
            ->order('num desc')
            ->find();
        if(!empty($questionnaire_user)){
            $num2=$questionnaire_user['num']+1;
        }

        if($num2<=3) {//问卷做三次
            foreach ($questions as $info) {
                $option_arr = array();
                foreach ($info['option'] as $option) {
                    if ($option['avatar'] == 1) {
                        $option_arr[] = $option['id'];
                    }
                }

                if (!empty($option_arr)) {
                    $option_arr = implode(",", $option_arr);
                } else {
                    $option_arr = '';
                }
                $insData = array();
                $insData['questionnaire_id'] = $questionnaire_id;
                $insData['issue_id'] = $info['id'];
                $insData['user_id'] = $user_id;
                $insData['value'] = $option_arr;
                $insData['num'] = $num2;
                $insData['createtime'] = time();
                $insData['updatetime'] = time();
                $res = Db::name('questionnaire_user')->insert($insData);
            }
        }

        $this->success('提交成功', $num);
    }

    //提交问卷
    public function submit_multiple()
    {
        $params = $this->request->post();
        $user = $this->auth->getUser();
        $user_id=$user->id;
        $questionnaire_id=$params['id'];
        $questions=$params['questions'];

        $num2=1;//问卷已做次数
        $questionnaire_user=Db::name('questionnaire_user')
            ->where('questionnaire_id',$questionnaire_id)
            ->where('user_id',$user_id)
            ->order('num desc')
            ->find();

        if(!empty($questionnaire_user)){
            $num2=$questionnaire_user['num']+1;
        }

        if($num2<=3) {//问卷做三次
            foreach ($questions as $info) {
                $option_arr = array();
                foreach ($info['option'] as $option) {
                    if ($option['avatar'] == 1) {
                        $option_arr[] = $option['id'];
                    }
                }

                if (!empty($option_arr)) {
                    $option_arr = implode(",", $option_arr);
                } else {
                    $option_arr = '';
                }
                $insData = array();
                $insData['questionnaire_id'] = $questionnaire_id;
                $insData['issue_id'] = $info['id'];
                $insData['user_id'] = $user_id;
                $insData['value'] = $option_arr;
                $insData['num'] = $num2;
                $insData['createtime'] = time();
                $insData['updatetime'] = time();
                $res = Db::name('questionnaire_user')->insert($insData);
            }
        }

        $this->success('提交成功', '');
    }

    //我的问卷列表
    public function my_questionnaire_list()
    {
        $params = $this->request->post();
        $user = $this->auth->getUser();
        $user_id=$user->id;

        $data=array();
        $questionnaire_user_list=Db::name('questionnaire_user')->alias('qu')
            ->join('questionnaire q','q.id=qu.questionnaire_id')
            ->where('qu.user_id',$user_id)
            ->field('q.id,q.title,qu.num,qu.createtime')
            ->group("CONCAT(CONCAT(q.id, ','),qu.num)")
            ->order('qu.createtime desc')
            ->select();

        $this->success('我的问卷列表', $questionnaire_user_list);
    }

    //我的问卷详情
    public function my_questionnaire_info()
    {
        $params = $this->request->post();
        $id=$params['id'];
        $num=$params['num'];
        $data=array();
        $questionnaire_data=array();

        $info=Db::name('questionnaire')
            ->where('id',$id)
            ->find();
        $data['info']=$info;

        $user = $this->auth->getUser();
        $user_id=$user->id;
        $questionnaire_user_list=Db::name('questionnaire_user')
            ->where('questionnaire_id',$id)
            ->where('user_id',$user_id)
            ->where('num',$num)
            ->select();

        $questionnaire=Db::name('questionnaire_issue')->alias('i')
            ->join('questionnaire_option o','o.issue_id=i.id')
            ->where('i.questionnaire_id',$id)
            ->field('i.id,i.title,i.is_multiple,o.id as o_id,o.title as o_title,o.is_correct')
            ->select();

        $id_arr=array();
        $hidden=false;
        foreach ($questionnaire as $key2=>$info){
            if(!in_array($info['id'],$id_arr)){
                $questions=array();
                $questions['id']=$info['id'];
                $questions['title']=$info['title'];//问题
                $questions['is_multiple']=$info['is_multiple'];//是否多选
                $questions['hidden']=$hidden;//是否隐藏，前端用
                if($hidden==false)$hidden=true;
                $questions['progress']=0;//进度，前端用
                $questions['option']=array();
                $id_arr[]=$info['id'];
                $questionnaire_data[]=$questions;
            }

            foreach ($questionnaire_data as $key=>$v){
                if($v['id']==$info['id']){
                    $option=array();
                    $option['id']=$info['o_id'];
                    $option['value']=$info['o_title'];
                    $option['is_correct']=$info['is_correct'];
                    $option['avatar']=0;//是否选中

                    foreach ($questionnaire_user_list as $qul){
                        if($qul['issue_id']==$info['id']){
                            $value=array();
                            if(!empty($qul['value']))$value=explode(",",$qul['value']);
                            if(in_array($info['o_id'],$value))$option['avatar']=1;//是否选中
                        }
                    }
                    $questionnaire_data[$key]['option'][]=$option;
                }
            }
        }

        $count=count($questionnaire_data);
        $progress=0;
        if($count!=0){$progress=round(100/$count,0);}
        foreach ($questionnaire_data as $key=>$v){
            if($key+1<$count){
                $questionnaire_data[$key]['progress']=$progress*($key+1);
            }else{
                $questionnaire_data[$key]['progress']=100;
            }
        }

        $data['count']=$count;
        $data['questionnaire']=$questionnaire_data;
        $data['num']=$num;

        $this->success('题目', $data);
    }
}